
Ext.require([
    'Ext.form.*',
    'Ext.tip.QuickTipManager',
    'Ext.data.*',
    'Ext.form.field.ComboBox'

]);


Ext.define('Writer.Person', {
    extend: 'Ext.data.Model',
    fields: [ {
			name: 'nombreRol'
		}]
});

Ext.onReady(function() {
    Ext.tip.QuickTipManager.init();

    var store = Ext.create('Ext.data.Store', {
        model: 'Writer.Person',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            url: '/listarRoles',
            reader: {
                type: 'json',
                successProperty: 'success',
                root: 'data',
                messageProperty: 'message'
            },
            writer: {
                type: 'json',
                writeAllFields: false,
                root: 'data'
            },

        }        
        
    });

    var formPanel = Ext.widget('form', {
        renderTo: 'asignarRol_extjs',
        frame: true,
        width: 350,
        x: 200,
        y: 80,
        bodyPadding: 20,
        bodyBorder: true,
        title: 'ASIGNAR ROLES',

        defaults: {
            anchor: '100%'
        },

        items: [{
            xtype: 'textfield',
            name: 'id_usuario',
            fieldLabel: 'Id Usuario',
            allowBlank: false,
           
        }, {
            xtype: 'combobox',
            name: 'roles',
            fieldLabel: 'Roles',
			multiSelect: true,
			displayField: 'nombreRol',
            store: store,
           
        }],

        dockedItems: [{
            xtype: 'container',
            dock: 'bottom',
            layout: {
                type: 'hbox',
                align: 'middle'
            },
            padding: '5 0 5',

            items: [{
                xtype: 'button',
                formBind: true,
                disabled: true,
                text: 'ASIGNAR',
                width: 140,
                handler: function() {
                    var form = this.up('form').getForm();
                    form.submit({
                        url: '/asignarRol',	
						params: {
								submit: 'submit'
						},
						method: "post",
						success: function(form, action) {			
                            Ext.Msg.alert('Rol asignado satisfactoriamente');
                        },
                        failure: function(form, action) {
                            Ext.Msg.alert('Error al asiganr el rol');
                        }		
						
                    });

                    
                    
                }
            },
            {
                xtype: 'button',
                text: 'CANCELAR',
                width: 140,
                handler: function() {
						window.location = "main";
                    
                }
            }
            ]
        }]
    });
    



});

